
    <div class="content margin_top">
        <header class="header fixed_top bg_mx_green"></header>
        <form class="inspection"></form>
    </div>
    <div class="toast"></div>

    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=GKTGV62UVGc1FZb4wUBdWG8w"></script>
    <!-- toast模版 -->
    <script type="text/template" charset="utf-8" id='toast_template'>
        <i class="iconfont icon-toast{{=it.icon}}"></i>
        <div class="toast_text">{{=it.text}}</div>
    </script>

    <script type="text/template" charset="utf-8" id='info_template'>
        <div class="bg_white margin_b_10">

            <div class="padding_h_15 padding_v_5 border_b row lineheight_3">
                {{for(var i in it.input){}}
                <div class="col_6 s_text flex" style="{{if(it.input[i].viewwidth == 'full'){}}width:100%;{{}}}border-bottom: 1px solid #eee;">
                    <div class="width_70">{{=it.input[i].name}}</div>
                    {{if(it.input[i].type=='input'){}}
                        <div class="left_70 light">
                            <input {{=it.input[i].event}}="{{=it.input[i].eventtrigger}}"  class="checkinput" value="{{=it.input[i].default}}" type="text" id="{{=it.input[i].name}}" name="{{=it.input[i].name}}" placeholder="{{=it.input[i].placeholder||'请输入'}}" request title="{{=it.input[i].name}}" {{if(it.input[i].datatype=='int'){}} data-pattern="^[0-9]+(.[0-9]{1,2})?$" data-tips="最多两位小数的数字"{{}else if(it.input[i].ltype=='char'){}} data-pattern="^[\u4e00-\u9fa5_a-zA-Z]+$" data-tips="非数字的字符"{{}}}>
                        </div>
                    {{}else if(it.input[i].type=='select'){}}
                        <div class="left_70 light">
                            <select {{=it.input[i].event}}="{{=it.input[i].eventtrigger}}" class="checkinput"  request title="{{=it.input[i].name}}"  name="{{=it.input[i].name}}" id="{{=it.input[i].name}}">
                                {{for(var j in it.input[i].data){}}
                                    <option value="{{=it.input[i].data[j]}}" {{if(it.input[i].default == it.input[i].data[j]){ }} selected {{ } }}>{{=it.input[i].data[j]}}</option>
                                {{}}}
                            </select>
                        </div>
                    {{}}}
                </div>
                {{}}}
            </div>
        </div>

        {{for(var i in it.textarea){}}
            <div class="bg_white margin_b_10">
                <div class="text_info padding_10_15 border_b">
                    <textarea  class="checkinput" name="{{=it.textarea[i].name}}" placeholder="{{=it.textarea[i].placeholder}}" request title="{{=it.textarea[i].name}}">{{=it.textarea[i].default}}</textarea>
                </div>
            </div>
        {{}}}

        {{for(var i in it.image){}}
            <div class="bg_white margin_b_10">
                <div class="padding_15">
                    <div class="preview{{=i}} flex_row_3 margin_b_10"></div>
                    <div class="light lineheight_3 v_center input2">
                        <div class="width_50 border text_center">
                            <div class="iconfont icon-add font_20"></div>
                            <input class="absolute all" type="file" value="" accept="image/*" style="opacity:0;" onchange="selectpic(this,{{=i}})"/>
                            <input class="checkinput" type="hidden" id="{{=it.image[i].name}}" name="{{=it.image[i].name}}" request value="" title="{{=it.image[i].name}}"/>
                        </div>
                        <div class="left_50  padding_l_15">{{=it.image[i].placeholder}}</div>
                    </div>
                </div>
            </div>
            {{}}}

        <div class="padding_15 font_16">
            <button class="btn btn_mx_green all_width lineheight_3">保存</button>
        </div>
        <input name="_csrf" type="hidden" id="_csrf" value="<{$app->request->csrfToken}>">
        <!--是否补检-->
        <input type="hidden" name="bujian" value="<{$bujian}>">
        <input type="hidden" name="insid" value="<{$insid}>">

        <input type="hidden" name="pigid">
        <input type="hidden" name="province">
        <input type="hidden" name="city">
        <input type="hidden" name="district">
        <input type="hidden" name="street">
        <input type="hidden" name="lng">
        <input type="hidden" name="lat">
    </script>
    <script type="text/template" charset="utf-8" id='img_template'></script>
    <script>
        // 页面初始化
            var n = <{$items}>;console.log(n)
            var evalinfo = doT.template($("#info_template").text());
            var evalToast = doT.template($("#toast_template").text());
            loaddata(".inspection",evalinfo,n);
            loadheadbar("常规巡检",1)
        // 选择图片
            var img_arr=[[],[],[]],imgs=[[],[],[]],evalImg;
            loadimg(function(){
                evalImg = doT.template($("#img_template").text())
            });
            function selectpic(obj,i){
                var file = obj.files[0];
                var URL = window.URL || window.webkitURL;
                tobase64(file,function(res){
                    imgs[i].push(res);
                })
                img_arr[i].push(URL.createObjectURL(file))
                $(".preview"+i).html(evalImg({n:i,d:img_arr[i]}));console.log(img_arr[i])
                $(obj).val("");
                if(img_arr[i].length>=3){
                    $(".input"+i).hide();
                }
            }
        // 删除图片
            function del(n,i){
                imgs[n].splice(i,1);
                img_arr[n].splice(i,1);
                $(".preview"+n).html(evalImg({n:n,d:img_arr[n]}));
                $(".input"+n).show();
            }
        // 定位
            var geolocation = new BMap.Geolocation();
            geolocation.getCurrentPosition(function(r){
                if(this.getStatus() == BMAP_STATUS_SUCCESS){
                    $("input[name=lng]").val(r.point.lng)
                    $("input[name=lat]").val(r.point.lat)
                    $("input[name=province]").val(r.address.province)
                    $("input[name=city]").val(r.address.city)
                    $("input[name=district]").val(r.address.district)
                    $("input[name=street]").val(r.address.street)
                }else {
                    alert('failed');
                }
            },{enableHighAccuracy: true})
        // 提交表单
            var submited = false;
            $(".inspection").submit(function(e){
                e.preventDefault();
                if(!confirm('确认提交')){
                    return false;
                }
                var pigid = GetQueryString("pigid");
                $("input[name=pigid]").val(pigid);

                with(this){
                    $.each(n.image,function (index,v) {
//                        (n.image[v].name).value = imgs[index].join("%^");
                        console.log(v.name)
                        $('#'+v.name).val(imgs[index].join("%^"));
                    })
                    var checkno = true;
                    $('.checkinput').each(function (index,v) {
                        if($(v).attr('request') == '' && $(v).val() == ''){
                            toast("wrong","请输入"+$(v).attr('title'));
                            $(v).focus();
                            checkno = false;
                            return false;
                        }
                    })
                    if(!checkno){
                        return false;
                    }
                    if(submited) return false;
                    var data = $(".inspection").serialize();
                    $.post("/business/person/new-post-inspection",data,function(res){console.log(res)
                        submited = true;
                        toast("attention",res.msg);
                        if(res.status == 0){
                            location.href='/business/person/pig';
                        }
                    },"json")
                }
            });



        $('#breath').change(function(){
                var val = $(this).val();
                var statext = '呼吸<span style="font-size: 12px;color: red;">(正常)</span>';
                if(val>=10 && val<=20){
                    statext = '呼吸<span style="font-size: 12px;color: red;">(正常)</span>';
                }else if(val>20 && val<=30){
                    statext = '呼吸<span style="font-size: 12px;color: red;">(急促)</span>';
                }else if(val>=1 && val<10){
                    statext = '呼吸<span style="font-size: 12px;color: red;">(缓慢)</span>';
                }
                $(this).parent().prev('.width_70').html('');
                $(this).parent().prev('.width_70').append(statext);
        })

        
        $(document).ready(function () {
            var val = $('#season').val();
            init_pen_temperature(val);
            $('#season').change(function(){
                val = $(this).val();
                init_pen_temperature(val);
            });
            ;

            
            function init_pen_temperature(val) {
                var statext = '';
                if(val=='春季'){
                    var min = 5;
                    var max = 25;
                }else if(val=='夏季'){
                    var min = 30;
                    var max = 43;
                }else if(val=='秋季'){

                    var min = 5;
                    var max = 35;
                }else if(val=='冬季'){
                    var min = -10;
                    var max = 10;
                }
                for(var i=min;i<=max;i++){
                    console.log(parseInt((max+min)/2))
                    if(i==parseInt((max+min)/2)){
                        statext += '<option value='+i+' selected>'+i+'</option>';
                    }else{
                        statext += '<option value='+i+'>'+i+'</option>';

                    }
                }
                $('#pen_temperature').html(statext);
            }


            $('#width').blur(function(){
                var weight = parseInt($('#width').val())*parseInt($('#length').val())/$('#xishu').val();
                $('#weight').val(weight.toFixed(2));
            })

            $('#length').blur(function(){
                var weight = parseInt($('#width').val())*parseInt($('#length').val())/$('#xishu').val();
                $('#weight').val(weight.toFixed(2));
            })

            //根据公式计算体重
            $('#xishu').change(function(){
                var weight = parseInt($('#width').val())*parseInt($('#length').val())/$(this).val();
                if(isNaN(weight)){
                    if($('#width').val() == ''){
                        toast("wrong","胸围不能为空");
                        $('#width').focus();
                        return false
                    }
                    if($('#length').val() == ''){
                        toast("wrong","体长不能为空");
                        $('#length').focus();
                        return false;
                    }
                }
                $('#weight').val(weight.toFixed(2));
            });

        })




    </script>


